package com.yonyougov.devops.cruxcisupport.thread;

import com.yonyougov.devops.cruxcisupport.app.AppDao;
import com.yonyougov.devops.cruxcisupport.backupuser.BackupTestUserDao;
import com.yonyougov.devops.cruxcisupport.backupuser.BackupTestUserEntity;
import com.yonyougov.devops.cruxcisupport.commitcontext.CommitContextService;
import com.yonyougov.devops.cruxcisupport.commitcontext.vo.CommitContextInfoVo;
import com.yonyougov.devops.cruxcisupport.committest.CommitTestDao;
import com.yonyougov.devops.cruxcisupport.committest.CommitTestEntity;
import com.yonyougov.devops.cruxcisupport.enums.TestStatusEnum;
import com.yonyougov.devops.cruxcisupport.subscribe.SubscribeDao;
import com.yonyougov.devops.cruxcisupport.subscribe.SubscribeEntity;
import com.yonyougov.devops.cruxcisupport.user.UserDao;
import com.yonyougov.devops.cruxcisupport.user.UserEntity;
import com.yonyougov.devops.cruxcisupport.util.MailUtils;
import com.yonyougov.devops.cruxcisupport.version.VersionDao;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 发送 驳回/通过/发版 邮件
 */
public class SendRejectOrPassOrReleaseMailThread extends Thread{
    private MailUtils mailUtils;
    private String remark;
    private BigInteger commitTestId;
    private SubscribeDao subscribeDao;
    private VersionDao versionDao;
    private AppDao appDao;
    private CommitTestDao commitTestDao;
    private BackupTestUserDao backupTestUserDao;
    private String from;
    private UserDao userDao;
    private String currrentUserName;
    private List<String> attachmentUrls;
    private int status;
    private List<BigInteger> toUserList;
    private List<String> excellPaths;
    private CommitContextService commitContextService;

    public SendRejectOrPassOrReleaseMailThread(MailUtils mailUtils, BigInteger commitTestId, SubscribeDao subscribeDao,
                                               VersionDao versionDao, CommitTestDao commitTestDao, AppDao appDao,
                                               String from, String remark, BackupTestUserDao backupTestUserDao, UserDao userDao,
                                               String currrentUserName, List<String> attachmentUrls, int status,
                                               List<BigInteger> toUserList, List<String> excellPaths,
                                               CommitContextService commitContextService) {
        this.mailUtils = mailUtils;
        this.commitTestId = commitTestId;
        this.remark = remark;
        this.subscribeDao = subscribeDao;
        this.versionDao = versionDao;
        this.commitTestDao = commitTestDao;
        this.appDao = appDao;
        this.from = from;
        this.backupTestUserDao = backupTestUserDao;
        this.userDao = userDao;
        this.currrentUserName = currrentUserName;
        this.attachmentUrls = attachmentUrls;
        this.status = status;
        this.toUserList = toUserList;
        this.excellPaths = excellPaths;
        this.commitContextService = commitContextService;
    }

    @Override
    public void run() {
        //根据提测Id查询提测记录
        CommitTestEntity commitTestEntity = new CommitTestEntity();
        commitTestEntity.setId(this.commitTestId);
        CommitTestEntity commitTest = commitTestDao.findBy(commitTestEntity);
        List<CommitContextInfoVo> contextInfoVos = commitContextService.
                findCommitContextByCommitTestId(commitTestEntity.getId());
        StringBuffer appName = new StringBuffer();
        StringBuffer versionName = new StringBuffer();
        StringBuffer branchName = new StringBuffer();
        Set<BigInteger> appIds = new HashSet<>();
        contextInfoVos.forEach(c -> {
            appName.append(c.getAppName()).append(";");
            versionName.append(c.getVersionName()).append(";");
            branchName.append(c.getTestBranch()).append(";");
            appIds.add(c.getAppId());
        });
        //查询订阅人
        SubscribeEntity subscribeEntity = new SubscribeEntity();
        //subscribeEntity.setAppId(commitTest.getAppId());
        List<SubscribeEntity> subscribeEntityList = subscribeDao.findAllBy(subscribeEntity);
        //查询备测人
        BackupTestUserEntity backupTestUserEntity = new BackupTestUserEntity();
        backupTestUserEntity.setCommitTestId(commitTestId);
        List<BackupTestUserEntity> backupTestUserEntityList = backupTestUserDao.findAllBy(backupTestUserEntity);
        //获取收件人的邮箱地址
        Set<BigInteger> userIds = new HashSet<>();
        userIds.add(commitTest.getSubmitUserId());
        userIds.add(commitTest.getTestLead());
        if(null != subscribeEntityList && subscribeEntityList.size()>0){
            subscribeEntityList.forEach(subscribe -> userIds.add(subscribe.getUserId()));
        }
        if(null != backupTestUserEntityList && backupTestUserEntityList.size()>0){
            backupTestUserEntityList.forEach(backupTestUser -> userIds.add(backupTestUser.getTestUserId()));
        }
        // 发版时，自定义收件人
        if (status == TestStatusEnum.RELEASE.getValue()) {
            if (null == toUserList) toUserList = new ArrayList<>();
            userIds.addAll(toUserList);
            userIds.add(commitTest.getTestLead());
        }
        //根据用户Id查询用户信息
        List<UserEntity> userEntityList = userDao.findByIds(userIds);
        //获取邮箱列表
        String[] tos = userEntityList.stream().map(UserEntity::getEmail).toArray(String[] :: new);
        //发送邮件
        String subject = "";
        String content = "";
        if (this.status == TestStatusEnum.TEST_REJECT.getValue()) {
            subject = "【提测】测试驳回";
            content = "测试人： " + currrentUserName + " : " + appName + " 的 " + versionName + " 测试未通过!原因: " + this.remark;
        } else if (this.status == TestStatusEnum.TEST_SUCCESS.getValue()) {
            subject = "【提测】测试通过";
            content = "测试人： " + currrentUserName + " : " + appName + " 的 " + versionName + " 测试通过!详情如下: " + this.remark;
        } else if (this.status == TestStatusEnum.RELEASE.getValue()) {
            subject = "【提测】测试发版";
            content = "发版人： " + currrentUserName + " : " + appName + " 的 " + versionName + " 测试发版!详情如下: " + this.remark;
            if (excellPaths != null && excellPaths.size() > 0) {
                attachmentUrls.addAll(excellPaths);
            }
        }
        mailUtils.sendEmailWithAttachments(this.from,tos,subject,content,attachmentUrls);
    }
}
